package com.jh.paymentchannel.pojo;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConstants;
import com.alipay.api.DefaultAlipayClient;
import com.fasterxml.jackson.annotation.JsonFormat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

import static org.springframework.util.StringUtils.hasText;

/**
 * 支付宝配置表
 */
@Entity
@Table(name="t_alipay_info_no")
public class AlipayLifeNo implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@Id
	@Column(name="id")
	private long id;

	/**
	 * 支付宝应用APPID
	 */
	@Column(name="app_id")
	private String appid;

	/**
	 * 支付宝商户类型
	 */
	@Column(name="alipay_type")
	private String alipayType;

	/**
	 * 商家支付宝ID
	 */
	@Column(name="partner")
	private String partner;

	/**
	 * 商户id
	 */
	@Column(name="shop_id")
	private String shopId;


	/**
	 * 支付宝公钥
	 */
	@Column(name="alipay_public_key")
	private String alipayPublicKey;


	/**
	 * 支付宝授权token
	 */
	@Column(name="authorization_token")
	private String authorizationToken;

	/**
	 * 私钥
	 */
	@Column(name="private_key")
	private String privateKey;

	/**
	 * 公钥
	 */
	@Column(name="public_key")
	private String publicKey;
	
	@Column(name="create_time")
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

	@Column(name="create_by")
	private String createBy;

	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name="update_time")
	private String updateTime;

	@Column(name="update_by")
	private String updateBy;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getPartner() {
		return partner;
	}

	public void setPartner(String partner) {
		this.partner = partner;
	}

	public String getAlipayPublicKey() {
		return alipayPublicKey;
	}

	public void setAlipayPublicKey(String alipayPublicKey) {
		this.alipayPublicKey = alipayPublicKey;
	}

	public String getPrivateKey() {
		return privateKey;
	}

	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}

	public String getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getAlipayType() {
		return alipayType;
	}

	public void setAlipayType(String alipayType) {
		this.alipayType = alipayType;
	}

	public String getAuthorizationToken() {
		return authorizationToken;
	}

	public void setAuthorizationToken(String authorizationToken) {
		this.authorizationToken = authorizationToken;
	}

	public String getCreateBy() {
		return createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}

	public String getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(String updateBy) {
		this.updateBy = updateBy;
	}


	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public AlipayClient defaultAlipayClient(String serverUrl) {
	    // 注意：是支付宝公钥，不是应用公钥
		return new DefaultAlipayClient(serverUrl, getAppid(), getPrivateKey(), AlipayConstants.FORMAT_JSON,
				AlipayConstants.CHARSET_UTF8, getAlipayPublicKey(), AlipayConstants.SIGN_TYPE_RSA2);
	}

	public boolean valid() {
		return hasText(getPartner()) && hasText(getPrivateKey()) && hasText(getAlipayPublicKey());
	}
}
